package org.mclll.xwangplugin.util;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.mclll.xwangplugin.town.config.TownContext.TOWN_DETAIL_LINE_1;
import static org.mclll.xwangplugin.town.config.TownContext.TOWN_DETAIL_LINE_2;

/**
 * @author: geduo
 * @date: 2023/9/23 19:30
 */
public class PlayerTools {
    public static Component getPeopleInfo(List<TownPlayerDO> townPlayerList, Collection<? extends Player> onlinePlayers) {
//        List<BaseComponent> peopleShowList = Lists.newArrayList();
        TextComponent.Builder peopleShowList = Component.text();
        int onLineCount = 0;
        TextComponent.Builder leaderLine = Component.text();
        for (int i = 0; i < townPlayerList.size(); i++) {
            if (i != 0) {
                peopleShowList.appendNewline();
            }
            TownPlayerDO player = townPlayerList.get(i);
            boolean onlineStatus = getOnlineStatus(onlinePlayers, player.getUuid());
            if (onlineStatus) {
                onLineCount++;
            }
            String onlineStatusStr = onlineStatus ? "§a【在线】" : "§c【离线】";
            if (i == 0 && player.getIdentity() == PlayerTownIdentity.LEADER) {
//                peopleShowList.add(new TextComponent("§b" + player.getName() + "【镇长】" + (onlineStatus ? "§a【在线】" : "§c【离线】")));
                peopleShowList.append(Component.text(String.format("§b%s【镇长】%s", player.getName(), onlineStatusStr)));
                leaderLine.appendNewline().append(Component.text(String.format(TOWN_DETAIL_LINE_2, player.getName())));
            } else {
//                peopleShowList.add(new TextComponent("\n§b" + player.getName() + (onlineStatus ? "§a【在线】" : "§c【离线】")));
                peopleShowList.append(Component.text(String.format("§b%s%s", player.getName(), onlineStatusStr)));
            }
        }
//        TextComponent peopleInfo = new TextComponent("§b" + townPlayerList.size() + "/" + onLineCount);
//        peopleInfo.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, peopleShowList.toArray(new BaseComponent[0])));
//        return Lists.newArrayList(new TextComponent("\n" + TOWN_DETAIL_LINE_1), peopleInfo);
        return Component.text().appendNewline()
                .append(Component.text(TOWN_DETAIL_LINE_1))
                .append(Component.text(String.format("§b%d/%d", townPlayerList.size(), onLineCount)).hoverEvent(HoverEvent.showText(peopleShowList.build())))
                .append(leaderLine.build())
                .build();
    }

    public static boolean getOnlineStatus(Collection<? extends Player> onlinePlayers, String uuid) {
        for (Player onlinePlayer : onlinePlayers) {
            if (onlinePlayer.getUniqueId().toString().equals(uuid)) {
                return true;
            }
        }
        return false;
    }

    public static List<String> getPlayerNameList() {
        List<String> playName = Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
        Arrays.stream(Bukkit.getOfflinePlayers()).map(org.bukkit.OfflinePlayer::getName).forEach(playName::add);
        return playName;
    }

    public static OfflinePlayer getOfflineOrOnlinePlayerByUuid(String uuid) {
        UUID targetPlayerUuid = UUID.fromString(uuid);
        OfflinePlayer targetPlayer = Bukkit.getPlayer(targetPlayerUuid);
        if (targetPlayer == null) {
            targetPlayer = Bukkit.getOfflinePlayer(targetPlayerUuid);
        }
        return targetPlayer;
    }

    public static OfflinePlayer getOfflineOrOnlinePlayerByName(String name) {
        OfflinePlayer targetPlayer = Bukkit.getServer().getPlayer(name);
        if (targetPlayer == null) {
            targetPlayer = Bukkit.getOfflinePlayer(name);
        }
        return targetPlayer;
    }

    public static void addPermission(String playerName, String permission) {
        String command = "lp user %s permission set %s true";
        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format(command, playerName, permission));
    }

    public static void delPermission(String playerName, String permission) {
        String command = "lp user %s permission set %s false";
        Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format(command, playerName, permission));
    }
}
